import 'package:cult/function/utils/my_common.dart';
import 'package:flutter/material.dart';
import 'package:ducafe_ui_core/ducafe_ui_core.dart';
import 'package:get/get.dart';
import 'package:flutter_svg/flutter_svg.dart';
import '../navigation_controller.dart';

/// 自定义底部导航栏组件
/// 使用GetX进行页面逻辑分离
class CustomBottomNavWidget extends GetView<PrimaryScreenController> {
  const CustomBottomNavWidget({super.key});

  @override
  Widget build(BuildContext context) {
    return Obx(() {
      // 监听当前选中的标签页
      final currentIndex = controller.currentTabIndex.value;

      return Container(
        margin: EdgeInsets.only(left: 16.w, right: 16.w, bottom: 54.h),
        width: 352.w,
        height: 82.h,
        child: Stack(
          children: [
            // 背景图片
            Positioned.fill(
              child: Image.asset(
                _getBackgroundAsset(currentIndex),
                fit: BoxFit.fill,
              ),
            ),
            // 导航项容器
            Padding(
              padding: EdgeInsets.only(top: 12.h),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  _buildNavigationItem(0, 'home', 'Home'),
                  _buildNavigationItem(1, 'communal', 'Community'),
                  _buildNavigationItem(2, 'chat', 'Chat'),
                  _buildNavigationItem(3, 'my', 'Profile'),
                ],
              ),
            ),
          ],
        ),
      );
    });
  }

  /// 构建导航项
  Widget _buildNavigationItem(
    int index,
    String iconAssetName,
    String labelText,
  ) {
    return Obx(() {
      final isCurrentlySelected = controller.currentTabIndex.value == index;

      return GestureDetector(
        onTap: () => controller.switchToTab(index),
        child: SizedBox(
          width: 88.w,
          height: 82.h,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              // 图标
              SvgPicture.asset(
                'assets/images/$iconAssetName.svg',
                width: 24.w,
                height: 24.w,
                colorFilter: ColorFilter.mode(
                  isCurrentlySelected
                      ? MyCommonUtils.green
                      : MyCommonUtils.black,
                  BlendMode.srcIn,
                ),
              ),
              SizedBox(height: 4.h),
              // 文字标签
              Text(
                labelText,
                style: TextStyle(
                  fontSize: 12.sp,
                  color:
                      isCurrentlySelected
                          ? MyCommonUtils.green
                          : MyCommonUtils.black,
                  fontWeight: FontWeight.w700,
                ),
              ),
            ],
          ),
        ),
      );
    });
  }

  /// 获取背景图片资源
  String _getBackgroundAsset(int selectedIndex) {
    switch (selectedIndex) {
      case 0:
        return 'assets/images/home_selected.png';
      case 1:
        return 'assets/images/communal_selected.png';
      case 2:
        return 'assets/images/chat_selected.png';
      case 3:
        return 'assets/images/my_selected.png';
      default:
        return 'assets/images/home_selected.png';
    }
  }
}
